package top.sssd.auth.service.impl.userRole;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import top.sssd.auth.service.userRole.UserRoleService;
import top.sssd.auth.mapper.userRole.UserRoleMapper;
import top.sssd.auth.model.userRole.converter.UserRoleConverter;
import top.sssd.auth.model.userRole.entity.UserRole;
import top.sssd.auth.model.userRole.request.UserRoleQueryReq;
import top.sssd.auth.model.userRole.request.UserRoleSaveOrUpdateReq;
import top.sssd.auth.model.userRole.response.UserRoleResp;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

@Service
@RequiredArgsConstructor
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements UserRoleService {

    @Override
    public Page<UserRoleResp> queryPage(UserRoleQueryReq userroleQueryReq) {
        Page<UserRole> page = new Page<>(userroleQueryReq.getPageNum(), userroleQueryReq.getPageSize());
        Page<UserRole> resultPage = commonQuery(userroleQueryReq).page(page);
        List<UserRoleResp> resultList = resultPage.getRecords().parallelStream()
                .map(UserRoleConverter.INSTANCE::entityToResp)
                .toList();
        return new Page<UserRoleResp>()
                .setTotal(resultPage.getTotal())
                .setCurrent(resultPage.getCurrent())
                .setSize(resultPage.getSize())
                .setRecords(resultList);
    }

    @Override
    public List<UserRoleResp> queryUserRoleRespListByUserRoleReq(UserRoleQueryReq UserRoleQueryReq) {
        List<UserRole> list = commonQuery(UserRoleQueryReq).list();
        return list.parallelStream()
                .map(UserRoleConverter.INSTANCE::entityToResp)
                .toList();
    }

    private LambdaQueryChainWrapper<UserRole> commonQuery(UserRoleQueryReq userroleQueryReq) {
        return this.lambdaQuery()
                .eq(ObjectUtil.isNotEmpty(userroleQueryReq.getId()), UserRole::getId, userroleQueryReq.getId())
                .eq(ObjectUtil.isNotEmpty(userroleQueryReq.getUserId()), UserRole::getUserId, userroleQueryReq.getUserId())
                .eq(ObjectUtil.isNotEmpty(userroleQueryReq.getRoleId()), UserRole::getRoleId, userroleQueryReq.getRoleId())
                .orderByDesc(UserRole::getUpdatedTime);
    }

    @Override
    public UserRoleResp getUserRoleRespById(Serializable id) {
        UserRole userrole = this.getById(id);
        if (Objects.nonNull(userrole)) {
            return UserRoleConverter.INSTANCE.entityToResp(userrole);
        }
        throw new RuntimeException("用户角色关联表不存在");
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Boolean saveUserRoleReq(UserRoleSaveOrUpdateReq userroleSaveOrUpdateReq) {
        UserRole userrole = UserRoleConverter.INSTANCE.saveOrUpdateUserRoleReqToEntity(userroleSaveOrUpdateReq);
        return this.save(userrole);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Boolean updateUserRoleReq(UserRoleSaveOrUpdateReq userroleSaveOrUpdateReq) {
        UserRole userrole = UserRoleConverter.INSTANCE.saveOrUpdateUserRoleReqToEntity(userroleSaveOrUpdateReq);
        return this.updateById(userrole);
    }
}

